#!/usr/bin/perl -w
#
# File:    fig2egm.pl
# Purpose: Converts an XFig file into an electrogladiator FIG.  See
#          http://www-epb.lbl.gov/xfig/fig-format.html for details on the
#          fig format.
#
# Copyright 2009 James Youngquist, Colin Scott.
#
# This file is part of ElectroGladiator.  ElectroGladiator is free
# software: you can redistribute it and/or modify it under the terms
# of the GNU General Public License as published by  the Free
# Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

use strict;
use POSIX;

print <<EOF;
This program converts an XFig fig format save file into an ElectroGladiator
compatible map.

EOF

#die ("Usage: $0 <file.fig>\n") if ($ARGV != 0);

my $fig_file = $ARGV[0];    # name of fig file
my ($egm_file) = split( /\./, $fig_file );
$egm_file .= ".egm";        # name of electrogladiator map file 
my $num_lines = 0;          # total number of interior map lines
my $cur_line  = 0;          # current line we're on
my @lines;                  # array holding all lines

# open files
open( FIG, $fig_file )     || die("Couldn't open $fig_file for reading: $!");
open( EGM, "> $egm_file" ) || die("Couldn't open $egm_file for writing: $!");

print "Processing $fig_file...";

# ignore initial comments in FIG
while (<FIG>) {
	last if (/^[^#]/);
}

chomp( my $orientation       = $_ );
chomp( my $justification     = <FIG> );
chomp( my $units             = <FIG> );
chomp( my $papersize         = <FIG> );
chomp( my $magnification     = <FIG> );
chomp( my $multiple_page     = <FIG> );
chomp( my $transparent_color = <FIG> );

# ignore optional comments in FIG
while ( chomp( $_ = <FIG> ) ) {
	last if (/^[^#]/);
}

my ( $resolution, $coord_system ) = split( /\s/, $_ );
$resolution /= 10; # because 10 inches = 100 units.  arbitrary.

# process the rest of FIG into EGM
while (<FIG>) {
	chomp;
	my @object      = split(/\s+/);    # get object's fields
	my $object_code = $object[0];

	# perl style switch
	for ($object_code) {
		if (/2/) {
			my $sub_type   = $object[1];   # not used right now
			my $num_points = $object[-1];  # total num points in line def
			my $points_left = $num_points; # num points remaining to process
			my @this_line;                 # points that make up this line

			$num_lines += $num_points - 1; # fig lines are screwy like that

			my @read_line = split( /\s+/, <FIG> );    # read in all points
			while ($points_left > 6) {
				@read_line = (@read_line, split( /\s+/, <FIG> ));
				$points_left -= 6;
			}
			foreach (@read_line) {
				if (/\d+/) {
				    push @this_line, $_;
				}
			}
			my $x1        = (shift @this_line) / $resolution;
			my $y1        = (shift @this_line) / $resolution;
			my $x2        = (shift @this_line) / $resolution;
			my $y2        = (shift @this_line) / $resolution;

			$lines[$cur_line++] = "l,$x1,$y1,$x2,$y2\n";

			for ( 1 .. $num_points - 2 ) {
				$x1 = $x2;
				$y1 = $y2;
				$x2 = (shift @this_line) / $resolution;
				$y2 = (shift @this_line) / $resolution;
				$lines[$cur_line++] = "l,$x1,$y1,$x2,$y2\n";
			}
		}
	}
}

print "Done.\n";

print "Writing $egm_file...";
print EGM "100\n"; #hardcoded limits right now
print EGM "100\n";
print EGM "$num_lines\n";
foreach (@lines) {
	print EGM;
}
print "Done.\n";

close FIG;
close EGM;